package com.hsy.sunflowerbao.message.ui

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View

import com.flux.action.RxStoreChange
import com.hsy.sunflowerbao.R
import com.hsy.sunflowerbao.base.action.ActionType
import com.hsy.sunflowerbao.base.ui.fragment.BaseFragment
import com.hsy.sunflowerbao.home.action.HomeActionCreator
import com.hsy.sunflowerbao.message.store.MessageStore
import com.hsy.sunflowerbao.utils.CommonUtils
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import kotlinx.android.synthetic.main.layout_refresh_recycle.*

/**
 * 消息Fragment
 * Created on 2017/12/4.
 *
 * @author yang
 */
class MessageFragment : BaseFragment<MessageStore>(), OnRefreshLoadMoreListener {

    private var failureView: View? = null
    /**
     * 事件产生器
     */
    private val creator = HomeActionCreator(dispatcher, manager)
    /**
     * 数据缓存器
     */
    override var store = MessageStore(dispatcher)
    /**
     * 布局文件
     */
    override val fragmentLayout: Int
        get() = R.layout.layout_refresh_recycle

    /**
     * 我的问答适配器
     */
//    private val adapter = HomeAdapter(this, listOf())

    private var isCreate = false
    private var init = true

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        isCreate = true
    }

    override fun initView(view: View?) {
        refresh.setOnRefreshListener(this)
        recycle.layoutManager = LinearLayoutManager(recycle.context)
//        recycle.adapter = adapter
    }

    override fun initData() {
        super.initData()
        if (!init) {
//            CommonUtils.showProgressDialog(context)
//            onRefresh(refresh)
        }
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (isVisibleToUser) {
            init = isCreate
            if (isCreate) {
//                CommonUtils.showProgressDialog(context)
//                onRefresh(refresh)
            }
        }
    }

    /**
     * 重载刷新事件
     */
    override fun onRefresh(refresh: RefreshLayout) {
//        creator.getHomeData()
    }

    override fun onLoadMore(refreshLayout: RefreshLayout?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    /**
     * 界面响应统一处理
     * @param change 需要界面响应的变化
     */
    override fun onRxStoreChanged(change: RxStoreChange) {
        if (change.storeId == store.tag) when (change.type) {
            ActionType.GET_USER_INFO -> {
                CommonUtils.hideProgressDialog()
                refresh.finishRefresh()
                if (change.success) {
                    if (failureView != null) {
                        homeParent.removeView(failureView)
                        failureView = null
                    }
//                    adapter.items = store.qaResult.data
//                    adapter.notifyDataSetChanged()
//                    listEmptyView.visibility = if (adapter.items.isNotEmpty()) View.GONE else View.VISIBLE
                } else {
                    failureView = context?.let { CommonUtils.showNetworkFailure(it, homeParent, View.OnClickListener { onFragmentRefresh() }) }
                }
            }
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        isCreate = false
    }
}